#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#

import socket
import struct
import subprocess

class Ftp(object):
    def list(self):
        return "list"

    def get(self):
        print("下载")

    def put(self):
        print("上传")

ftp = Ftp()
dic = {
    "1": ftp.list,
    "2": ftp.get,
    "3": ftp.put
}

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect_ex(("127.0.0.1", 2008))
    while True:
        print("\t列出：list\n\t上传upload local_file_name\n\t下载download file_name")
        inp = input(">>>: ").strip()
        if not inp: continue
        client.send(inp.encode("utf-8"))
        inp = inp.split(" ")[0]
        print(inp, type(inp))
        if inp == "list":
            header_size = client.recv(4)
            total_size = struct.unpack("i", header_size)[0]
            recv_size = 0
            res = b""
            while recv_size < total_size:
                data = client.recv(1024)
                res += data
                recv_size += len(data)
            print(res.decode("gbk"))
        elif inp == "upload":
            print(client.recv(1024).decode("utf-8"))
            # print(client.recv(1024).decode("utf-8"))
        else:
            print("oh no")